package com.nopay.cdxphone.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nopay.cdxphone.bean.AreaBean;

import java.util.ArrayList;
import java.util.List;

import com.nopay.cdxphone.R;

public class AreaCityAdapter extends BaseAdapter {

    private List<AreaBean.City> datas = new ArrayList();
    private LayoutInflater mInflater;

    public AreaCityAdapter(Context context) {
        super();
        mInflater = LayoutInflater.from(context);
    }

    public void addList(List<AreaBean.City> datas) {
        if(datas == null) return;

        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void clear() {
        this.datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public AreaBean.City getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_area, null);
            holder.tv_area = convertView.findViewById(R.id.tv_area);
            holder.iv_area = convertView.findViewById(R.id.iv_area);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        AreaBean.City bean = getItem(position);
        holder.tv_area.setText(bean.getName());
        holder.tv_area.setTextColor(Color.parseColor(bean.isSelected() ? "#178AED" : "#333333"));
        holder.iv_area.setVisibility(bean.isSelected() ? View.VISIBLE : View.INVISIBLE);

        return convertView;
    }

    public class ViewHolder {
        private TextView tv_area;
        private ImageView iv_area;
    }
}
